﻿package { 
import flash.display.Sprite; 

import com.adobe.images.JPGEncoder;
import com.adobe.serialization.json.JSON;
import com.adobe.serialization.json.JSONDecoder;
import com.adobe.serialization.json.JSONEncoder;
import com.alert.*;

import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.utils.ByteArray;
import flash.utils.Timer;

import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.events.TimerEvent;
import flash.events.Event; 
import flash.events.DataEvent;
import flash.events.MouseEvent; 
import fl.controls.Button; 
import fl.controls.TextInput;
import fl.controls.Label;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.navigateToURL;
import flash.net.FileReference; 
import flash.net.FileFilter; 
import com.ui.Imgzoom;
import com.ui.ImageCut;
  
public class Main extends Sprite { 

private var _pic:Loader = new Loader();
private var loader:URLLoader;
private var _bit:Bitmap;
private var image:ImageCut;
private var a:Sprite = new Sprite();
private var b:Sprite = new Sprite();
private var c:Sprite = new Sprite();
private var picbox:Sprite = new Sprite();
private var kuang:Sprite = new Sprite();
private var zoom:Zooms = new Zooms();

private var w:Number = 300;//width
private var h:Number = 320;//height
private var bmpData:BitmapData;
private var tmpRectangle:Rectangle;
private var bitmap1:Bitmap//大图
private var bitmap2:Bitmap//小图
private var bitmap3:Bitmap//小图
private var scale:Number = 1;//缩放比例
private var scale_x:Number = 1;
private var scale_y:Number = 1;
private var Yzuobiao:Number;
private var zoomx:Number;
private var zoomy:Number;
private var contentWidth:Number;//图片实际宽度
private var contentHeight:Number;//你猜
private var uid:String = String(stage.loaderInfo.parameters["uid"]);
private var url:String = String(stage.loaderInfo.parameters["url"]);
private var ipath:String = String(stage.loaderInfo.parameters["ipath"]);
	
public var file:FileReference; 
public var btn:Button; 

public var tput:TextInput;
public var lb:Label;
public var arr:Array; 
public var imgfilter:FileFilter; 
public var swffilter:FileFilter; 
public var ureq:URLRequest = new URLRequest; 
private var tag:Boolean = false;//drag
private var zoomTag:Boolean = false;//zoom drag
private var saveTag:Boolean = true;
private var _TextFormat:TextFormat = new TextFormat("Verdana", 13,false, false, false, false, '', '', TextFormatAlign.LEFT, 0, 0, 0, 0);

public function Main() {
	DataManager.STAGE_ROOT = this;
	init();
	//var c:Object = stage.loaderInfo.parameters;
	if(uid){
		loadPic(ipath);
	}
	
	
	//Alert.show('sadfsadf');
}
private function init():void{
	//trace(stage.loaderInfo.parameters.data);
	Alert.msg('加载。。。');
var sp:Sprite = new Sprite();

a.graphics.beginFill(0xffffff);
a.graphics.drawRect(0,0,100,100);
a.graphics.endFill();
a.x = 310;
a.y = 90;
addChild(a);
b.graphics.beginFill(0xffffff);
b.graphics.drawRect(0,0,80,80);
b.graphics.endFill();
b.x = 310;
b.y = 200;
addChild(b);
c.graphics.beginFill(0xffffff);
c.graphics.drawRect(0,0,50,50);
c.graphics.endFill();
c.x = 310;
c.y = 290;
addChild(c);

addChild(picbox);
file = new FileReference(); 
btn = new Button(); 
var save:Button = new Button();
var cancel:Button = new Button();
tput = new TextInput();
lb = new Label();
imgfilter = new FileFilter("Image", "*.png;*.jpg;*.gif;*.jpeg"); 


sp.graphics.beginFill(0xcdffff);
sp.graphics.drawRect(0,0,560,30);
addChild(sp);
btn.label = "上传头像"; 
//btn.label.color= "red";
btn.emphasized = true; 
btn.width = 60; 
btn.height = 20;	
btn.move(180, 5); 
save.label = "保存";
save.width = 45;
save.height = 20;
save.move(10,400);
cancel.label = "取消";
cancel.width = 45;
cancel.height = 20;
cancel.move(65,400);
lb.text = "支持jpg、jpeg、gif、png格式，文件小于2M。";
lb.width = 300;
lb.move(255,5);
lb.setStyle("textFormat",_TextFormat);
tput.x = 5;
tput.y = 5;
tput.text = "选择照片";
tput.buttonMode = true;
tput.editable = false;
tput.alwaysShowSelection = true;
tput.width = 170;
tput.setStyle('textFormat',_TextFormat);
//tput.height =24;

ureq.url = url; 
  
tput.addEventListener(MouseEvent.MOUSE_DOWN,down);
btn.addEventListener(MouseEvent.MOUSE_DOWN, down); 
save.addEventListener(MouseEvent.MOUSE_DOWN,savedown);
cancel.addEventListener(MouseEvent.MOUSE_DOWN,canceldown);
file.addEventListener(Event.COMPLETE, complete); 
file.addEventListener(Event.OPEN,open); 
file.addEventListener(Event.CANCEL, oncancel);//点击取消按钮会广播这个事件 
file.addEventListener(Event.SELECT, select); 
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeResponse);
  
sp.addChild(btn); 
sp.addChild(save);
sp.addChild(cancel);
sp.addChild(tput);
sp.addChild(lb);
Alert.msgClose();
} 
  
public function down(e:MouseEvent):void { 
file.browse([imgfilter]);//browse的参数是以个FileFilter对象，限制文件选择的类型 
} 
public function complete(e:Event):void { 
trace("complete"+e); 
Alert.msgClose();
} 
public function open(e:Event):void { 
trace("open"); 
} 
public function oncancel(e:Event):void { 
trace("canceled"); 
} 
public function select(e:Event):void { 
var tem = e.target; 
trace(tem.name + "," + tem.creationDate + "," + tem.size + ","); 
file.upload(ureq);//选择结束后，开始上传

Alert.msg('图片加载中，请稍后。。。');
} 
public function completeResponse(e:DataEvent):void{
trace(e);
	var j:Object = JSON.decode(e.data);
	loadPic(j.path);
} 
public function loadPic(picdata:String):void{
	Alert.msg('图片加载中，请稍后。。。');
	_pic.contentLoaderInfo.addEventListener(Event.COMPLETE,loadok);
	trace('..'+picdata);
	_pic.load(new URLRequest('..'+picdata));
}
public function loadok(e:Event):void{
	Alert.msgClose();
	dispatchEvent(new Event(Event.COMPLETE));        
    (_pic.content as Bitmap).smoothing = true;
	contentWidth = _pic.content.width;
    contentHeight = _pic.content.height;
	if (_pic.width * h > _pic.height * w) {   
        _pic.width = w;             
        scale = w/contentWidth;
        _pic.height = contentHeight * scale;     
        scale_x = scale;
    }else {
        _pic.height = h;
        scale = h/contentHeight;
        _pic.width = contentWidth * scale;
		scale_y = scale;
    }
	_pic.x = 5+(150-_pic.width/2);
	_pic.y = 50+(160-_pic.height/2);

	picbox.graphics.lineStyle(1, 0xcccccc, 1,true, "normal", "square", "miter", 10);
	picbox.graphics.moveTo(4, 49);
    picbox.graphics.lineTo(w+5, 49);
    picbox.graphics.lineTo(w+5, h+50);
    picbox.graphics.lineTo(4, h+50);
    picbox.graphics.lineTo(4, 49);

   picbox.addChild(_pic);
   kuang.graphics.clear();
   zoomx = 0;
   zoomy = 0;
   if(_pic.width<150||_pic.height<150){  
   	  zoomx = _pic.height<_pic.width?_pic.height:_pic.width;
	  zoomy = _pic.height<_pic.width?_pic.height:_pic.width;
   }else{
	  zoomx = 150;
  	  zoomy = 150;
   }
   kuang.graphics.lineStyle(2, 0xffff00, 1);
   kuang.graphics.beginFill(0xccff00,0);
   kuang.graphics.drawRect(0,0,zoomx,zoomy);
   kuang.graphics.endFill();

   kuang.x = 5+w/2-zoomx/2;
   kuang.y = 50+h/2-zoomy/2;
 	
    kuang.addEventListener(MouseEvent.MOUSE_DOWN,kuangDown);
    stage.addEventListener(MouseEvent.MOUSE_MOVE,kuangMove);
	stage.addEventListener(MouseEvent.MOUSE_UP,kuangUp);
	kuang.addEventListener(Event.ENTER_FRAME,kuangEnterFrame);
	kuang.buttonMode = true;
   	addChild(kuang);

  	zoom.x = zoomx-8;
	zoom.y = zoomy-8;
  	kuang.addChild(zoom);
    zoom.addEventListener(MouseEvent.MOUSE_DOWN,zoomDown);
	zoom.addEventListener(Event.ENTER_FRAME,zoomEnterFrame);
    bmpData= new BitmapData(kuang.width/scale,kuang.height/scale);
    tmpRectangle = new Rectangle(0,0,kuang.width/scale,kuang.height/scale);
	
	bmpData.draw(_pic,new Matrix(1, 0, 0, 1, -Math.floor((kuang.x-5)/scale), -Math.floor((kuang.y-50-(h-_pic.height)/2)/scale )),null,null,tmpRectangle);
	
    bitmap1 = new Bitmap(bmpData);
    bitmap1.smoothing = true;
    bitmap1.x = a.x;
    bitmap1.y = a.y;
	var cutZoom1:Imgzoom = new Imgzoom(bitmap1.width,bitmap1.height,a.width,a.height);
	bitmap1.width = cutZoom1.width()<a.width?a.width:cutZoom1.width();
	bitmap1.height = cutZoom1.height()<a.height?a.height:cutZoom1.height();
    addChild(bitmap1);
   
    bitmap2 = new Bitmap(bmpData);
    bitmap2.smoothing = true;
    bitmap2.x = b.x;
    bitmap2.y = b.y;
	var cutZoom2:Imgzoom = new Imgzoom(bitmap2.width,bitmap2.height,b.width,b.height);
	bitmap2.width = cutZoom2.width()<b.width?b.width:cutZoom2.width();
	bitmap2.height = cutZoom2.height()<b.height?b.height:cutZoom2.height();
    addChild(bitmap2);
	
	bitmap3 = new Bitmap(bmpData);
    bitmap3.smoothing = true;
    bitmap3.x = c.x;
    bitmap3.y = c.y;
	var cutZoom3:Imgzoom = new Imgzoom(bitmap3.width,bitmap3.height,c.width,c.height);
	bitmap3.width = cutZoom3.width()<c.width?c.width:cutZoom3.width();
	bitmap3.height = cutZoom3.height()<c.height?c.height:cutZoom3.height();
    addChild(bitmap3);
}

public function kuangDown(e:MouseEvent):void{
	tag = true;
}
public function kuangMove(e:MouseEvent):void{
	var _rectangle:Rectangle = new Rectangle();
	if(zoomTag){
		tag = false;
		_rectangle.x = 8;
		_rectangle.y = 8;
		_rectangle.height = _pic.height-kuang.y+50+(h-_pic.height)/2-16;
		_rectangle.width = _pic.width-kuang.x+5+(w-_pic.width)/2-16;
		zoom.startDrag(false,_rectangle);
	}
	if(tag){
		zoomTag = false;
		_rectangle.x = 5+(w-_pic.width)/2;
		_rectangle.y = 50+(h-_pic.height)/2;
		_rectangle.height = _pic.height-kuang.height+1;
		_rectangle.width = _pic.width-kuang.width+1;
		kuang.startDrag(false,_rectangle);
		if(stage.mouseX>(5+_pic.width)||stage.mouseX<5||stage.mouseY>(50+h-(h-_pic.height)/2||stage.mouseY<(50+(h-_pic.height)/2))){
			tag = false;
			kuang.stopDrag();
		}
	}
}
public function kuangUp(e:MouseEvent):void{
	tag = false;
	zoomTag = false;
	kuang.stopDrag();
	zoom.stopDrag();
	//zoom.x = kuang.width;
	//.y = kuang.height;
}
public function kuangEnterFrame(e:Event):void{
	if(tag){
		bmpData.draw(_pic,new Matrix(1, 0, 0, 1, -Math.floor((kuang.x-5)/scale), -Math.floor((kuang.y-50-(h-_pic.height)/2)/scale )),null,null,tmpRectangle);
		bitmap1.bitmapData = bmpData;
		bitmap2.bitmapData = bmpData;
	}
} 
public function savedown(e:Event):void{
	if(saveTag){
		saveTag = false;
	var jpg:JPGEncoder = new JPGEncoder(80);
	//var jpgStream:ByteArray = bmpData.getPixels(new Rectangle(0,0,150,150));
	var jpgStream:ByteArray = jpg.encode(bmpData);
	var request:URLRequest = new URLRequest(ureq.url);
	request.method = URLRequestMethod.POST;
	request.data = jpgStream;
	loader= new URLLoader();
	loader.dataFormat = URLLoaderDataFormat.TEXT;
	loader.addEventListener(Event.COMPLETE,completeHander);
	loader.load(request);
	Alert.msg('保存中。。。');
	}
}
public function canceldown(e:Event):void{
var url:URLRequest = new URLRequest("javascript:window.history.go(-1);"); 
navigateToURL(url,'_self');
}
private function completeHander(e:Event):void{
	Alert.msgClose();
	
	//trace(loader.data);
	var j:Object = JSON.decode(loader.data);
	//Alert.msg(e);Alert.msg('sadfsadf',true);
	Alert.msg(j.info);
	var t:Timer = new Timer(3000,1);
	t.addEventListener(TimerEvent.TIMER,timerHandler);
	t.start();
	saveTag = true;
	//setTimeout(Alert.msgClose,3000);
	trace(e);
}
private function timerHandler(e:TimerEvent):void{
	Alert.msgClose();
	var url:URLRequest = new URLRequest("javascript:window.location.reload();");
	navigateToURL(url,'_self');
}
public function zoomDown(e:MouseEvent):void{
	trace('zoom down');
	trace('kang.x'+kuang.x)
	zoomTag = true;
	tag = false;
	kuang.stopDrag();
}
public function zoomEnterFrame(e:Event):void{
	var r:Number;
	if(zoomTag){
		tag = false;
		if(zoom.x>zoom.y){
			kuang.width = zoom.y+8;
			kuang.height = zoom.y+8;
			zoom.x = zoom.y;
		}else{
			kuang.width = zoom.x+8;
			kuang.height = zoom.x+8;
			zoom.y = zoom.x;
		}
		r = kuang.width;
		kuang.graphics.clear();
		kuang.graphics.lineStyle(2, 0xffff00, 1);
   		kuang.graphics.beginFill(0xccff00,0);
   		kuang.graphics.drawRect(0,0,r,r);
   		kuang.graphics.endFill();
		
		bmpData= new BitmapData(kuang.width/scale,kuang.height/scale);
		tmpRectangle = new Rectangle(0,0,kuang.width/scale,kuang.height/scale);
		bmpData.draw(_pic,new Matrix(1, 0, 0, 1, -Math.floor((kuang.x-5)/scale), -Math.floor((kuang.y-50-(h-_pic.height)/2)/scale )),null,null,tmpRectangle);
		bitmap1.bitmapData = bmpData;
		bitmap2.bitmapData = bmpData;
		bitmap3.bitmapData = bmpData;
		var cutZoom1:Imgzoom = new Imgzoom(bitmap1.width,bitmap1.height,a.width,a.height);
		bitmap1.width = cutZoom1.width()<a.width?a.width:cutZoom1.width();
		bitmap1.height = cutZoom1.height()<a.height?a.height:cutZoom1.height();
		var cutZoom2:Imgzoom = new Imgzoom(bitmap2.width,bitmap2.height,b.width,b.height);
		bitmap2.width = cutZoom2.width()<b.width?b.width:cutZoom2.width();
		bitmap2.height = cutZoom2.height()<b.height?b.height:cutZoom2.height();
		var cutZoom3:Imgzoom = new Imgzoom(bitmap3.width,bitmap3.height,c.width,c.height);
		bitmap3.width = cutZoom3.width()<c.width?c.width:cutZoom3.width();
		bitmap3.height = cutZoom3.height()<c.height?c.height:cutZoom3.height();
	}
}
}
}